<!DOCTYPE html>
<html>
<head>
    <title>Physics Diagram Fig 4.8</title>
</head>
<body>
    <canvas id="physicsCanvas" width="800" height="420"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // Helper function to draw an arrow head
        function drawArrowhead(ctx, x, y, angle) {
            ctx.save();
            ctx.translate(x, y);
            ctx.rotate(angle);
            ctx.beginPath();
            ctx.moveTo(0, 0);
            ctx.lineTo(-10, 5);
            ctx.moveTo(0, 0);
            ctx.lineTo(-10, -5);
            ctx.stroke();
            ctx.restore();
        }

        // --- Setup ---
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 1.5;
        ctx.font = '18px sans-serif';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        // --- Figure (a) ---
        const scint_x = 150;
        const scint_y = 70;
        const scint_w = 40;
        const scint_h = 100;
        ctx.strokeRect(scint_x, scint_y, scint_w, scint_h);
        ctx.fillText('scintillator', scint_x + scint_w / 2, scint_y - 20);

        // Particle Beam
        const beam_y = scint_y + 50;
        ctx.beginPath();
        ctx.moveTo(40, beam_y);
        ctx.lineTo(scint_x + scint_w + 60, beam_y);
        ctx.stroke();
        drawArrowhead(ctx, scint_x + scint_w + 60, beam_y, 0);
        ctx.textAlign = 'left';
        ctx.fillText('particle', 45, beam_y - 20);
        ctx.fillText('beam', 45, beam_y);
        ctx.textAlign = 'center';

        // Photomultiplier Tube
        const pmt_top_y = scint_y + scint_h;
        ctx.beginPath();
        // Left side
        ctx.moveTo(scint_x, pmt_top_y);
        ctx.lineTo(scint_x - 10, pmt_top_y);
        ctx.quadraticCurveTo(scint_x - 20, pmt_top_y + 50, scint_x + 10, pmt_top_y + 80);
        ctx.lineTo(scint_x + 10, pmt_top_y + 100);
        // Right side
        ctx.moveTo(scint_x + scint_w, pmt_top_y);
        ctx.lineTo(scint_x + scint_w + 10, pmt_top_y);
        ctx.quadraticCurveTo(scint_x + scint_w + 20, pmt_top_y + 50, scint_x + scint_w - 10, pmt_top_y + 80);
        ctx.lineTo(scint_x + scint_w - 10, pmt_top_y + 100);
        ctx.stroke();

        // PMT Label and Arrow
        ctx.textAlign = 'left';
        ctx.fillText('photomultiplier tube', scint_x + scint_w + 20, pmt_top_y + 100);
        
        ctx.beginPath();
        const arrow_start_x = scint_x + scint_w + 15;
        const arrow_start_y = pmt_top_y + 95;
        const arrow_end_x = scint_x + scint_w - 5;
        const arrow_end_y = pmt_top_y + 70;
        const cp_x = arrow_start_x - 40;
        const cp_y = arrow_start_y - 30;
        ctx.moveTo(arrow_start_x, arrow_start_y);
        ctx.quadraticCurveTo(cp_x, cp_y, arrow_end_x, arrow_end_y);
        ctx.stroke();
        const tangent_angle = Math.atan2(arrow_end_y - cp_y, arrow_end_x - cp_x);
        drawArrowhead(ctx, arrow_end_x, arrow_end_y, tangent_angle);

        // Figure (a) label
        ctx.textAlign = 'center';
        ctx.fillText('(a)', scint_x + scint_w / 2, pmt_top_y + 125);

        // --- Figure (b) - Literal Copy ---
        const origin_x = 550;
        const origin_y = 200;

        // Initial particle (n, p)
        ctx.beginPath();
        ctx.moveTo(origin_x - 120, origin_y);
        ctx.lineTo(origin_x, origin_y);
        ctx.stroke();
        ctx.fillText('n', origin_x - 130, origin_y);

        ctx.beginPath();
        ctx.moveTo(origin_x - 80, origin_y);
        ctx.lineTo(origin_x - 60, origin_y);
        ctx.stroke();
        drawArrowhead(ctx, origin_x - 60, origin_y, 0);
        ctx.fillText('p', origin_x - 45, origin_y - 2);

        // Axes from interaction point
        // Horizontal axis (n)
        ctx.beginPath();
        ctx.moveTo(origin_x, origin_y);
        ctx.lineTo(origin_x + 150, origin_y);
        ctx.stroke();
        drawArrowhead(ctx, origin_x + 150, origin_y, 0);
        ctx.fillText('n', origin_x + 160, origin_y);
        
        // Vertical axis labeled p'
        ctx.beginPath();
        ctx.moveTo(origin_x, origin_y);
        ctx.lineTo(origin_x, origin_y - 100);
        ctx.stroke();
        drawArrowhead(ctx, origin_x, origin_y - 100, -Math.PI / 2);
        ctx.fillText("p'", origin_x + 12, origin_y - 100);

        // Dashed line at angle theta
        const angle_theta_deg = 28;
        const angle_theta_rad = angle_theta_deg * Math.PI / 180;
        const line_len = 120;
        const line_end_x = origin_x + line_len * Math.cos(angle_theta_rad);
        const line_end_y = origin_y - line_len * Math.sin(angle_theta_rad);
        
        ctx.save();
        ctx.setLineDash([5, 5]);
        ctx.beginPath();
        ctx.moveTo(origin_x, origin_y);
        ctx.lineTo(line_end_x, line_end_y);
        ctx.stroke();
        ctx.restore();

        // Angle theta
        const arc_radius = 50;
        ctx.beginPath();
        ctx.arc(origin_x, origin_y, arc_radius, 0, -angle_theta_rad, true);
        ctx.stroke();
        ctx.fillText('θ', origin_x + 55, origin_y - 25);
        
        // Figure (b) label
        ctx.fillText('(b)', origin_x + 80, origin_y + 100);

        // Main Figure Caption
        ctx.font = '20px sans-serif';
        ctx.fillText('Fig. 4.8', 375, 390);

    </script>
</body>
</html>